package cn.com.checne.controller.front;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.com.checne.constants.Constants;
import cn.com.checne.dao.GuestBookInfoDao;
import cn.com.checne.model.domain.GuestBookInfo;
import cn.com.checne.util.HeaderUtils;
import cn.com.checne.util.LanguageUtils;
import cn.com.checne.util.PageBean;
import cn.com.checne.util.StringTools;
import cn.com.checne.util.WebUtils;

@Controller
public class GuestBookFrontController {
	protected static final Logger logger = Logger.getLogger(GuestBookFrontController.class);

	@Resource
	private GuestBookInfoDao gbDao;


	//留言板输入页面
	@RequestMapping(value="/{lg}/gb",method=RequestMethod.GET)
	public ModelAndView index(
			@PathVariable String lg,
			HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		
		ModelAndView mav = new ModelAndView();
		lg = StringUtils.isBlank(lg) ? Constants.LANGUAGE_CN : lg ;
		String viewName = "front/" + lg + "/gb" ;
		int languageId = LanguageUtils.getLanguage(lg);
	    GuestBookInfo gb = new GuestBookInfo();
	    modelMap.put("gb", gb);
		modelMap.addAllAttributes(LanguageUtils.getModelMap(lg));
		modelMap.addAllAttributes(HeaderUtils.getHeaderMap(languageId));
		mav.setViewName(viewName);
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//留言内容保存
	@RequestMapping(value="/{lg}/gbSay",method=RequestMethod.POST)
	public ModelAndView postGB(
			@PathVariable String lg,
			HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		
		ModelAndView mav = new ModelAndView();
		lg = StringUtils.isBlank(lg) ? Constants.LANGUAGE_CN : lg ;
		String viewName = "front/" + lg + "/gb" ;
		int languageId = LanguageUtils.getLanguage(lg);
		String gbUsername = WebUtils.getRequestParameterAsString(request,"gbUsername");
		String gbEmail = WebUtils.getRequestParameterAsString(request,"gbEmail");
		String gbContent = WebUtils.getRequestParameterAsString(request,"gbContent");

		gbUsername = StringTools.re(gbUsername);
		gbEmail = StringTools.re(gbEmail);
		gbContent = StringTools.re(gbContent);
		GuestBookInfo gb = new GuestBookInfo();
		gb.setLanguageId(languageId);
		gb.setGbUsername(gbUsername);
		gb.setGbEmail(gbEmail);
		gb.setGbContent(gbContent);
		
		boolean errorUserName = (StringUtils.isBlank(gbUsername) || gbUsername.length() > 10) ? true : false ;
		boolean errorEmail = (StringUtils.isBlank(gbContent) || gbContent.length() > 30) ? true : false ;
		boolean errorContent = (StringUtils.isBlank(gbContent) || gbContent.length() > 1000) ? true : false ;
		boolean gbCreate = false ;
		if(!errorUserName && !errorEmail && !errorContent){
			gbCreate = gbDao.create(gb);
		}
	    modelMap.put("gb", gb);
	    modelMap.put("errorUserName", errorUserName);
	    modelMap.put("errorEmail", errorEmail);
	    modelMap.put("errorContent", errorContent);
	    modelMap.put("gbCreate", gbCreate);
		modelMap.addAllAttributes(LanguageUtils.getModelMap(lg));
		modelMap.addAllAttributes(HeaderUtils.getHeaderMap(languageId));
		mav.setViewName(viewName);
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//留言列表
	@RequestMapping(value="/{lg}/gbook",method=RequestMethod.GET)
	public ModelAndView gbList(
			@PathVariable String lg,
			@RequestParam(value = "curPage", required = false, defaultValue = "1") String pg,
			HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		
		ModelAndView mav = new ModelAndView();
		lg = StringUtils.isBlank(lg) ? Constants.LANGUAGE_CN : lg ;
		String viewName = "front/" + lg + "/gb-list" ;
		int languageId = LanguageUtils.getLanguage(lg);

		GuestBookInfoDao gbDao = new GuestBookInfoDao();
		int curPage = Integer.parseInt(pg);
		PageBean pageBean = new PageBean();
		pageBean.setCurPage(curPage);
		List<GuestBookInfo> gbList = gbDao.getGBList(languageId,1,-1,pageBean.getStart(),pageBean.getRowsPerPage());
		pageBean.setMaxRowCount(gbDao.getGBListCount(languageId,1,-1));
		pageBean.setMaxPage();
		pageBean.setPageNoList();
		
	    modelMap.put("gbList", gbList);
	    modelMap.put("pageBean", pageBean);
		modelMap.addAllAttributes(LanguageUtils.getModelMap(lg));
		modelMap.addAllAttributes(HeaderUtils.getHeaderMap(languageId));
		mav.setViewName(viewName);
		mav.addAllObjects(modelMap);
		return mav ;
	}
}